/*
 * NativeGeneratorTask.java  0.4.0 / Nov 5, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.tools.ng;

import java.io.IOException;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.launch.Launcher;
import org.apache.tools.ant.types.Path;

/**
 * Native Generator Ant Task.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class NativeGeneratorTask extends Task {

// Private Fields. -------------------------------------------------------------

  private boolean verbose;
  private Path srcDir;
  private String jniDir;
  private String delegateDir;

// Public Methods. -------------------------------------------------------------

  /**
   * Sets verbose mode.
   *
   * @param verbose the verbose
   */
  public void setVerbose(boolean verbose) {
    this.verbose = verbose;
  }

  /**
   * Sets source directory.
   *
   * @param srcDir the source directory
   */
  public void setSrcDir(Path srcDir) {
    if (this.srcDir == null) {
      this.srcDir = srcDir;
    } else {
      this.srcDir.append(srcDir);
    }
  }

  /**
   * Sets JNI directory.
   *
   * @param jniDir the JNI directory
   */
  public void setJniDir(String jniDir) {
    this.jniDir = jniDir;
  }

  /**
   * Sets Delegate directory.
   *
   * @param delegateDir the Delegate directory
   */
  public void setDelegateDir(String delegateDir) {
    this.delegateDir = delegateDir;
  }

  public void setDefualtGenerate(String defaultDenerate) {
    // TODO: implement
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  public void execute() throws BuildException {
    if (srcDir == null) {
      throw new BuildException("Attribute srcDir is required");
    }
    if (jniDir == null) {
      throw new BuildException("Attribute jniDir is required");
    }
    NativeGenerator generator = NativeGenerator.getInstance();
    generator.setVerbose(verbose);
    String[] src = srcDir.list();
    for (int i = 0; i < src.length; i++) {
      generator.addSourceTree(getProject().resolveFile(src[i]));
    }
    generator.setJniFolder(getProject().resolveFile(jniDir));
    if (delegateDir != null) {
      generator.setDelegateFolder(getProject().resolveFile(delegateDir));
    }
    try {
      generator.generate();
    } catch (IOException ioe) {
      throw new BuildException(ioe);
    }
  }

// Main Method. ----------------------------------------------------------------

  /**
   * Main test method.
   *
   * @param args the arguments
   */
  public static void main(String[] args) {
    Launcher.main(args);
  }

}
